﻿using System.Text.Json;

namespace K9Nano.Reporting;

/// <summary>
/// 报表参数信息
/// </summary>
public class ReportFormItem
{
    public const string DateStart = "K9Nano:DateStart";

    public const string DateEnd = "K9Nano:DateEnd";

    /// <summary>
    /// 参数变量名称
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 参数显示名称
    /// </summary>
    public string DisplayName { get; set; }

    /// <summary>
    /// 参数默认值
    /// </summary>
    public string DefaultValue { get; set; }

    /// <summary>
    /// 统计表单类型
    /// </summary>
    public EFormItemType FormItemType { get; set; }

    /// <summary>
    /// 参数SQL类型
    /// </summary>
    public ESqlParmaType SqlParmaType { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 当<see cref="FormItemType"/>为<see cref="EFormItemType.Dictionary"/>时，设置字典数据名称
    /// </summary>
    public string? Dictionary { get; set; }

    /// <summary>
    /// 当<see cref="FormItemType"/>为<see cref="EFormItemType.Select"/>时，设置选项列表
    /// </summary>
    public ReportFormItemOption[]? Options { get; set; }

    public ReportFormItem()
    {

    }

    public ReportFormItem(string name,
        string displayName,
        EFormItemType fieldType,
        string defaultValue,
        ESqlParmaType sqlParmaType,
        string? description = null,
        ReportFormItemOption[]? options = null,
        string? dictionary = null)
    {
        Name = name;
        DisplayName = displayName;
        Description = description;
        DefaultValue = defaultValue;
        FormItemType = fieldType;
        Options = options;
        Dictionary = dictionary;
        SqlParmaType = sqlParmaType;
    }

    public object GetSqlValue(string valueText)
    {
        switch (SqlParmaType)
        {
            case ESqlParmaType.Text:
                return valueText;
            case ESqlParmaType.Integer:
                {
                    if (int.TryParse(valueText, out var value)) return value;
                }
                break;
            case ESqlParmaType.Double:
                {
                    if (double.TryParse(valueText, out var value)) return value;
                }
                break;
            case ESqlParmaType.Date:
                {
                    if (DateStart.Equals(valueText, StringComparison.Ordinal))
                    {
                        return DateTime.Today;
                    }
                    if (DateEnd.Equals(valueText, StringComparison.Ordinal))
                    {
                        return DateTime.Today.AddDays(1);
                    }
                    if(DateTime.TryParse(valueText, out var value))
                    {
                        if (DateEnd.Equals(DefaultValue, StringComparison.Ordinal))
                        {
                            // 扩展日期到今天结束
                            return value.Date.AddDays(1);
                        }
                        return value.Date;
                    }
                }
                break;
            case ESqlParmaType.Boolean:
                if ("-1".Equals(valueText, StringComparison.Ordinal)) return -1;

                return "true".Equals(valueText, StringComparison.OrdinalIgnoreCase)
                    || "1".Equals(valueText, StringComparison.Ordinal)
                    ? 1 : 0;
        }

        throw new InvalidDataException($"参数 {Name} 的默认值不合法: {valueText}");
    }

    public object GetDefaultSqlValue()
    {
        return GetSqlValue(DefaultValue);
    }

    public object GetSqlValue(JsonElement jsonElement)
    {

        switch (FormItemType)
        {
            case EFormItemType.Text:
            case EFormItemType.Select:
            case EFormItemType.Date:
            case EFormItemType.Dictionary:
                {
                    var text = jsonElement.GetString() ?? string.Empty;
                    return GetSqlValue(text);
                }
            case EFormItemType.Integer:
            case EFormItemType.Boolean:
                {
                    if (jsonElement.TryGetInt32(out var val)) return val;
                }
                break;
            case EFormItemType.Double:
                {
                    if (jsonElement.TryGetDouble(out var val)) return val;
                }
                break;
        }
        throw new InvalidDataException($"参数 {Name} 的值不合法");
    }

    /// <summary>
    /// 创建日期型表单项
    /// </summary>
    /// <param name="name"></param>
    /// <param name="displayName"></param>
    /// <param name="extend">为true时，在日期后面加上23:59:59</param>
    /// <returns></returns>
    public static ReportFormItem CreateDatePicker(string name,
        string displayName,
        bool extend,
        string? description = null)
    {
        return new ReportFormItem(
            name,
            displayName,
            EFormItemType.Date,
            defaultValue: extend ? DateEnd : DateStart,
            sqlParmaType: ESqlParmaType.Date,
            description: description
            );
    }


}

public record ReportFormItemOption(string Label, string Value);